<!-- 

    这样便可

    imageUrl: ''

    <FileUpload action="file/upload" :imageUrl="imageUrl" @imageUrlChanged="imageUrlChanged" />

    imageUrlChanged(imageUrl) {
        this.imageUrl = imageUrl
    }
 -->

<script>

import { baseURL, baseImageResourceURL } from '@/utils/http.js'

export default {
    // 
    props: ['action', 'imageUrl'],
    emits: ['imageUrlChanged'],
    data() {
        return {
            url: ''
        }
    },
    // 计算属性，BaseURL = 'http://localhost:8090/' + 'file/upload'
    // 然后映射到 后端的RESTful上
    computed: {
        getActionUrl() {
            return baseURL + this.action
        }
    },
    // 初始化s
    mounted() {
        this.init()
    },

    methods: {
        // 只需要文件名，通过http://localhost:8090/upload + response.data就可以访问到图片了
        handleAvatarSuccess(response) {
            this.url = baseImageResourceURL + response.data
            this.$emit('imageUrlChanged', this.url)
        },
        // 初始化
        init() {
            this.url = this.imageUrl
        },

    },
    watchers: {
        // 监控url的改变
        url() {
            this.init()

        }
    }
}
</script>


<template>
    <el-upload class="avatar-uploader" :action="getActionUrl" :show-file-list="false" :on-success="handleAvatarSuccess">
        <img v-if="url" :src="url" class="avatar" />
        <el-icon v-else class="avatar-uploader-icon">
            <Plus />
        </el-icon>
    </el-upload>
</template>
    

<style scoped>
.avatar-uploader .avatar {
    width: 178px;
    height: 178px;
    display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
    border: 1px dashed var(--el-border-color);
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
    border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    text-align: center;
}
</style>